﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBScript;
using eBlog.Common;
using eBlog.Core;
using eBlog.Entity;

namespace eBlog.Web.Admin
{
    public partial class CommentsManage : PageBase
    {
        readonly IDBBase dal = DBFactory.GetDbProvider(); //初始化数据工厂

		protected void Page_Load(object sender, EventArgs e)
		{


            PageValidate.CheckAdminLogin(this);

            if (!Page.IsPostBack)
            {
                switch (action)
                {
                    case "add":
                    case "edit":
                        edit.Visible = true;
                        list.Visible = false;
                        ShowInfo();
                        break;
                    case "del":
                        delInfo();
                        break;
					case "show":
                        edit.Visible = false;
                        list.Visible = false;
                        //show.Visible = true;
                        break;
                    default:
                        edit.Visible = false;
                        list.Visible = true;
                        BindList();
                        break;
                }
            }
			else
            {
                if (action == "del")
                    delInfo();
            }

		}

		private void BindList()
        {
            int PageIndex;
            if (!int.TryParse(Request.Params["page"], out PageIndex)) { PageIndex = 1; }

            ArrayList attrs = dal.Page(PageIndex, PageSize).GetListAndCount<Comments>();

            List<Comments> lists = attrs[0] as List<Comments>;
            ListCount = (int)attrs[1];
            RPLIST.DataSource = lists;
            RPLIST.DataBind();
            PageNav.Text = Pager.PageInfo(ListCount, PageSize, PageIndex); 
        }

					
		public void btnSave_Click(object sender, EventArgs e)
		{
			ArrayList strErrs = new ArrayList(); //Add时候必须用::分开控件名
			this.edit.Visible = true;
			if(!PageValidate.IsNumber(txtTargetID.Text))
			{
				strErrs.Add("txtTargetID::TargetID必须输入数字整数！"); 
			}
			if(!PageValidate.IsNumber(txtParentID.Text))
			{
				strErrs.Add("txtParentID::ParentID必须输入数字整数！"); 
			}
			if(!PageValidate.IsNumber(txtUserID.Text))
			{
				strErrs.Add("txtUserID::UserID必须输入数字整数！"); 
			}
			if(this.txtName.Text =="")
			{
				strErrs.Add("txtName::Name不能为空，请输入字符！"); 
			}
			if(this.txtEmail.Text =="")
			{
				strErrs.Add("txtEmail::Email不能为空，请输入字符！"); 
			}
			if(this.txtSiteUrl.Text =="")
			{
				strErrs.Add("txtSiteUrl::SiteUrl不能为空，请输入字符！"); 
			}
			if(this.txtContent.Text =="")
			{
				strErrs.Add("txtContent::Content不能为空，请输入字符！"); 
			}
			if(this.txtIP.Text =="")
			{
				strErrs.Add("txtIP::IP不能为空，请输入字符！"); 
			}
			if(!PageValidate.IsDateTime(txtCreateDate.Text))
			{
				strErrs.Add("txtCreateDate::CreateDate必须输入时间格式2010-12-31，请选择日期时间！"); 
			}
			if(!PageValidate.IsNumber(txtStaus.Text))
			{
				strErrs.Add("txtStaus::Staus必须输入数字整数！"); 
			}

			if (strErrs.Count > 0) 
			{
				string[] arrayErrs = (string[])strErrs.ToArray(typeof(string));
				string strErr = string.Join("\\n", arrayErrs); 
				//MessageBox.Show(this,strErr);
				ErrCodeStr.Value = strErr;
				return;
			}

			int TargetID=int.Parse(this.txtTargetID.Text);
			int ParentID=int.Parse(this.txtParentID.Text);
			int UserID=int.Parse(this.txtUserID.Text);
			string Name=this.txtName.Text;
			string Email=this.txtEmail.Text;
			string SiteUrl=this.txtSiteUrl.Text;
			string Content=this.txtContent.Text;
			string IP=this.txtIP.Text;
			DateTime CreateDate=DateTime.Parse(this.txtCreateDate.Text);
			int Staus=int.Parse(this.txtStaus.Text);


			if (action == "edit" && id > 0)
			{
				Comments model = dal.GetModel<Comments>(id); //为了不覆盖更新,先读出存在的数据再按需填充数据

				model.TargetID=TargetID;
				model.ParentID=ParentID;
				model.UserID=UserID;
				model.Name=Name;
				model.Email=Email;
				model.SiteUrl=SiteUrl;
				model.Content=Content;
				model.IP=IP;
				model.CreateDate=CreateDate;
				model.Staus=Staus;


				dal.Update<Comments>(model);
				//MessageBox.ShowAndRedirect(this, "修 改 完 成!", Referrer.Value);
				return;
			}
			else
			{
				Comments model = new Comments();  //添加数据,准备新的实体

				model.TargetID=TargetID;
				model.ParentID=ParentID;
				model.UserID=UserID;
				model.Name=Name;
				model.Email=Email;
				model.SiteUrl=SiteUrl;
				model.Content=Content;
				model.IP=IP;
				model.CreateDate=CreateDate;
				model.Staus=Staus;


				Response.Redirect(Request.Url.AbsolutePath + "#" + dal.Add<Comments>(model).ToString()); //ADD并 返回当前Uri绝对地址不带参数
				return;
			}
			
		}

		private void ShowInfo()
		{
			try { this.Referrer.Value = Request.UrlReferrer.PathAndQuery; }  catch { }
			if (id > 0 && action == "edit")
			{
				Comments model = dal.GetModel<Comments>(id);

				this.lblID.Text=model.ID.ToString();
				this.txtTargetID.Text=model.TargetID.ToString();
				this.txtParentID.Text=model.ParentID.ToString();
				this.txtUserID.Text=model.UserID.ToString();
				this.txtName.Text=model.Name;
				this.txtEmail.Text=model.Email;
				this.txtSiteUrl.Text=model.SiteUrl;
				this.txtContent.Text=model.Content;
				this.txtIP.Text=model.IP;
				this.txtCreateDate.Text=model.CreateDate.ToString();
				this.txtStaus.Text=model.Staus.ToString();
				this.btnSave.Text = "编 辑";
			}

		}




		private void delInfo()
		{
			string ids = PageValidate.Form("checkallid");
            if (!String.IsNullOrEmpty(ids))
            {
                //dal.From("Comments", "").Where("id in (@ids)").SetString("ids", ids).Delete();
                dal.From("Comments", "").Where("id in ("+ids+")").Delete();
            }
            else
			{
			    dal.Delete<Comments>(id);
			}

			Response.Redirect(Request.Url.AbsolutePath); //返回当前Uri绝对地址不带参数
			return;
		}


    }
}

